package com.itheima.chapter03.service.impl;

import com.itheima.chapter03.entity.Discuss;
import com.itheima.chapter03.repository.DiscussRepository;
import com.itheima.chapter03.service.DiscussService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DiscussServiceImpl implements DiscussService {
    @Autowired
    private DiscussRepository discussRepository;

    @Override
    public Discuss findById(Integer id) {
        Optional<Discuss> optional = discussRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    @Override
    public List<Discuss> list(int aId, int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return discussRepository.getDiscussPaged(aId, pageable);
    }

}
